import numpy as np
import cv2


def draw_gaze(image_in_path, pitchyaw, image_out_path, thickness=2, color=(0, 0, 255)):
    """
    copy and overwrite from eth-xgaze
    Draw gaze angle on given image with a given eye positions.
    Args:
        image_in_path:输入的image地址
        pitchyaw:弧度制的pitch、yaw
        image_out_path: 存放结果的image地址
    """
    image_in = cv2.imread(image_in_path)
    if image_in is None:
        raise ValueError("error!!!")
    image_out = image_in
    (h, w) = image_in.shape[:2]
    length = np.min([h, w]) / 2.0
    pos = (int(w / 2.0), int(h / 2.0))
    if len(image_out.shape) == 2 or image_out.shape[2] == 1:
        # 灰度图和单通道图
        image_out = cv2.cvtColor(image_out, cv2.COLOR_GRAY2BGR)
    dx = -length * np.sin(pitchyaw[1]) * np.cos(pitchyaw[0])
    dy = -length * np.sin(pitchyaw[0])
    cv2.arrowedLine(image_out, tuple(np.round(pos).astype(np.int32)),
                   tuple(np.round([pos[0] + dx, pos[1] + dy]).astype(int)), color,
                   thickness, cv2.LINE_AA, tipLength=0.2)
    cv2.imwrite(image_out_path, image_out)


if __name__ == "__main__":
    print(1111)
    image_in_path = r'F:\gaze_summary\Image\1.jpg'
    pitchyaw = [-0.2063811419166216, 0.541233379452781]
    image_out_path = r'F:\gaze_summary\Image\2.jpg'
    draw_gaze(image_in_path, pitchyaw, image_out_path)
